<?php

namespace App\Http\Controllers;

use App\Http\Requests\InfoIdCardRequest;
use App\Http\Requests\InfoRequest;
use App\Http\Requests\SetPayPasswordRequest;
use App\Http\Requests\UpdatePasswordRequest;
use App\Http\Requests\UpdatePayPasswordRequest;
use App\Http\Resources\InviteCollection;
use App\Model\User;
use App\Traits\Password;
use Hash;
use Hashids\Hashids;
use Illuminate\Http\Request;

class UserController extends Controller
{
    use Password;
    /**
     * 实名认证信息
     *
     * @param InfoRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function info(InfoRequest $request)
    {
        $user                 = User::where('id', request()->user()->id)->first();
        $user->nationality    = $request->input('nationality');
        $user->document_type  = $request->input('document_type');
        $user->name           = $request->input('name');
        $user->id_card_number = $request->input('id_card_number');
        $user->sex            = $request->input('sex');
        $user->job            = $request->input('job');
        $user->local          = $request->input('local');
        $user->address        = $request->input('address');
        $user->status         = 2;
        if (! $user->save()) {
            return response()->json(['error' => '网络错误，请重试'], 500);
        }

        return response()->json(['message' => 'ok'], 200);
    }

    /**
     * 身份证照片提交
     *
     * @param InfoIdCardRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function infoIdCard(InfoIdCardRequest $request)
    {
        $idCard = [
            $request->input('id_card_image'),
            $request->input('id_card_back_image'),
            $request->input('id_card_front_image'),
        ];

        $user                = User::where('id', request()->user()->id)->first();
        $user->id_card_image = json_encode($idCard);
        $user->status        = 3;
        if (! $user->save()) {
            return response()->json(['error' => '网络错误，请重试'], 500);
        }

        return response()->json(['message' => 'ok'], 200);
    }

    /**
     *  设置交易密码
     *
     * @param SetPayPasswordRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setPayPassword(SetPayPasswordRequest $request)
    {
        // 判断交易密码登录密码是否一致
        if (Hash::check($request->input('pay_password'), request()->user()->password)) {
            return response()->json(['error' => '登录密码与交易密码不可一致'], 403);
        }
        request()->user()->pay_password = bcrypt($request->input('pay_password'));
        if (! request()->user()->save()) {
            return response()->json(['error' => '网络错误，请重试'], 500);
        }

        return response()->json(['message' => 'ok'], 200);
    }

    /**
     * 修改登录密码
     *
     * @param UpdatePasswordRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatePassword(UpdatePasswordRequest $request)
    {
        // 判断登录密码
        $this->checkPassword(request()->user(), $request->input('old_password'));
        // 判断两次输入密码
        if ($request->input('old_password') == $request->input('password')) {
            return response()->json(['error' => '不可与原密码一致'], 403);
        }
        // 判断交易密码登录密码是否一致
        if (Hash::check($request->input('password'), request()->user()->pay_password)) {
            return response()->json(['error' => '登录密码与交易密码不可一致'], 403);
        }
        request()->user()->password = bcrypt($request->input('password'));
        if (! request()->user()->save()) {
            return response()->json(['error' => '网络错误，请重试'], 500);
        }

        return response()->json(['message' => '修改成功'], 200);
    }

    // 修改交易密码
    public function updatePayPassword(UpdatePayPasswordRequest $request)
    {
        // 判断交易密码
        $this->checkPayPassword(request()->user(), $request->input('old_pay_password'));
        // 判断两次输入密码
        if ($request->input('old_pay_password') == $request->input('pay_password')) {
            return response()->json(['error' => '不可与原密码一致'], 403);
        }
        // 判断交易密码登录密码是否一致
        if (Hash::check($request->input('pay_password'), request()->user()->password)) {
            return response()->json(['error' => '登录密码与交易密码不可一致'], 403);
        }
        request()->user()->pay_password = bcrypt($request->input('pay_password'));
        if (! request()->user()->save()) {
            return response()->json(['error' => '网络错误，请重试'], 500);
        }

        return response()->json(['message' => '修改成功'], 200);
    }

    /**
     *  邀请链接
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function invite()
    {
        $hashids = new Hashids(
            config('services.hashids.salt'),
            config('services.hashids.minHashLength'),
            config('services.hashids.alphabet')
        );

        $invite_code = $hashids->encode(request()->user()->id);

        return response()->json(compact('invite_code'));
    }

    /**
     * 推荐人名单
     *
     * @return InviteCollection|\Illuminate\Http\JsonResponse
     */
    public function inviteList()
    {
        $user = User::select('mobile', 'created_at', 'status')->where('pid', request()->user()->id)->paginate();

        if(!$user->total()){
            return response()->json(['error'=>'没有推荐人'],404);
        }
        return new InviteCollection($user);
    }
}